package com.witmed.main

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import android.widget.AdapterView
import androidx.core.view.get
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.ViewPager.OnPageChangeListener
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.google.android.material.navigation.NavigationBarView
import com.google.android.material.navigation.NavigationView
import com.witmed.home.view.HomeFragment
import com.witmed.lib_common.ActivityPath
import com.witmed.main.adapter.FragAdapter
import com.witmed.main.databinding.ActivityMainBinding
import com.witmed.mine.view.MineFragment

@Route(path = ActivityPath.PAGE_MAIN)
class MainActivity : AppCompatActivity() {
    lateinit var db:ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        db=ActivityMainBinding.inflate(layoutInflater)
        setContentView(db.root)

        val homeFragment =
            ARouter.getInstance().build(ActivityPath.PAGE_HOME).navigation() as HomeFragment

        val mineFragment=
            ARouter.getInstance().build(ActivityPath.PAGE_MINE).navigation() as MineFragment

        val list = arrayListOf<Fragment>(homeFragment, mineFragment)
        val fragAdapter = FragAdapter(
            list,
            supportFragmentManager,
            FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
        )
        db.vp.adapter=fragAdapter

        db.vp.addOnPageChangeListener(object :OnPageChangeListener{
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {

            }

            override fun onPageSelected(position: Int) {
                db.nav.menu.getItem(position).setChecked(true)
            }

            override fun onPageScrollStateChanged(state: Int) {

            }

        })

        db.nav.setOnItemSelectedListener(object : NavigationBarView.OnItemSelectedListener {
            override fun onNavigationItemSelected(item: MenuItem): Boolean {
                if(item.itemId==R.id.home){
                    db.vp.currentItem=0
                }else{
                    db.vp.currentItem=1
                }
                return true
            }


        })
    }
}